<?php
function check_userwriter($userid){
    $sessionLifetime = 1800; //30min校验一次
    session_set_cookie_params([
        'lifetime' => $sessionLifetime,
        'path' => '/',
        'domain' => $_SERVER['HTTP_HOST'],
        'secure' => $_SERVER['HTTPS'] ?? false,
        'httponly' => false,
    ]);
    session_start(); 
    if (isset($_SESSION['authorized']) && $_SESSION['authorized'] === true && time() < $_SESSION['expires_at']) {
    echo json_encode(array('code' => 200, 'msg' => 'success'));
    } else {
    require('common.php');
    $sql = "select * from `novelwrite` where `writer`='$userid'";
    $result = $conn -> query($sql);
    if ($result -> num_rows > 0) {
        $row = $result -> fetch_assoc();
            if ($row['zg'] == 1) {  
                $_SESSION['authorized'] = true;
        $_SESSION['expires_at'] = time() + $sessionLifetime;
            } else{  
                session_write_close();  
                echo json_encode(array('code' => 400, 'msg' => '您未申请资格或未通过资格审核，请联系管理员'));
            } 
    } else {  
        session_write_close();  
        echo json_encode(array('code' => 400, 'msg' => '您未申请资格或未通过资格审核，请联系管理员'));
    } 
    }
    }
    $userid = $_GET['userid'];
    check_userwriter($userid);